/**
 * Created by jams on 2016/4/4.
 */

var gulp=require('gulp');//gulp基础库
// 引入 gulp及组件
var minifycss=	require('gulp-minify-css'), //css压缩
    concat=		require('gulp-concat'),//合并文件
    uglify=		require('gulp-uglyfly'),          //js压缩
    rename=		require('gulp-rename'),   //文件重命名s
    replace=	require("gulp-replace-async"),
//watch=		require("gulp-watch"),
//jshint=require('gulp-jshint'),   //js检查
    notify=		require('gulp-notify');   //提示

gulp.task('default',function(){
    gulp.start('css','js');
    //gulp.start('minifyjs');
});
gulp.task('watch',function(){
    "use strict";
    ['src/css/lib/*.css','src/css/xnb.css','src/js/lib/*.js','src/js/xnb.js'].forEach(function(src){
        var name=src.slice(src.lastIndexOf('.')+1);
        gulp.watch(src,[name]);
    });
});

//css处理
gulp.task('css',function(){
    return gulp.src(
        [
            'src/css/lib/*.css',
            'src/css/xnb.css'
        ])      //设置css
        .pipe(concat('xnb.css'))      //合并css文件
        .pipe(gulp.dest('public/pay/css'))           //设置输出路径
        .pipe(rename({suffix:'.min'}))         //修改文件名
        .pipe(minifycss())                    //压缩文件
        .pipe(gulp.dest('public/pay/css'))            //输出文件目录
        .pipe(notify({message:'css task ok'}));   //提示成功
});

//JS处理
gulp.task('js',function(){
    return gulp.src(
        [
            'src/js/lib/angular.js',
            'src/js/lib/angular-route.js',
            'src/js/lib/angular-touch.js',
            'src/js/lib/jquery-2.2.0.js',
            'src/js/lib/jquery.md5.js',
            'src/js/lib/jquery.sha1.js',
            'src/js/lib/extensions.js',
            'src/js/lib/rabbit.js',
            'src/js/xnb.js'
        ])  //选择合并的JS
        .pipe(concat('xnb.js'))   //合并js
        .pipe(gulp.dest('public/pay/js'))         //输出
        .pipe(rename({suffix:'.min'}))     //重命名
        .pipe(uglify())                    //压缩
        .pipe(replace(/([\u3400-\u9FFF])/g,function(match,callback){
            "use strict";
            //把中文替换成十六进制
            callback(null,"\\u"+match[0].charCodeAt(0).toString(16));
        }))
        .pipe(gulp.dest('public/pay/js'))            //输出
        .pipe(notify({message:"js task ok"}));    //提示
});